package com.zou.lotterylucky.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.zou.lotterylucky.R
import com.zou.lotterylucky.data.WinningLottery

class WinningLotteryAdapter(private var dataSet: MutableList<WinningLottery>) :
    RecyclerView.Adapter<WinningLotteryAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val dateLbl: TextView = view.findViewById(R.id.dateLbl)
        val kRank1Lbl: TextView = view.findViewById(R.id.kRank1Lbl)
        val kRank2Lbl: TextView = view.findViewById(R.id.kRank2Lbl)
        val kRank3Lbl: TextView = view.findViewById(R.id.kRank3Lbl)
        val fRank1Lbl: TextView = view.findViewById(R.id.fRank1Lbl)
        val fRank2Lbl: TextView = view.findViewById(R.id.fRank2Lbl)
        val fRank3Lbl: TextView = view.findViewById(R.id.fRank3Lbl)
        val dRank1Lbl: TextView = view.findViewById(R.id.dRank1Lbl)
        val dRank2Lbl: TextView = view.findViewById(R.id.dRank2Lbl)
        val dRank3Lbl: TextView = view.findViewById(R.id.dRank3Lbl)
        val stRank1Lbl: TextView = view.findViewById(R.id.stRank1Lbl)
        val stRank2Lbl: TextView = view.findViewById(R.id.stRank2Lbl)
        val stRank3Lbl: TextView = view.findViewById(R.id.stRank3Lbl)
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.item_winning_lottery, viewGroup, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        val winningLottery = dataSet[position]

        viewHolder.dateLbl.text = winningLottery.date
        viewHolder.kRank1Lbl.text = winningLottery.winningNumbers[0].rank1Number
        viewHolder.kRank2Lbl.text = winningLottery.winningNumbers[0].rank2Number
        viewHolder.kRank3Lbl.text = winningLottery.winningNumbers[0].rank3Number
        viewHolder.fRank1Lbl.text = winningLottery.winningNumbers[1].rank1Number
        viewHolder.fRank2Lbl.text = winningLottery.winningNumbers[1].rank2Number
        viewHolder.fRank3Lbl.text = winningLottery.winningNumbers[1].rank3Number
        viewHolder.dRank1Lbl.text = winningLottery.winningNumbers[2].rank1Number
        viewHolder.dRank2Lbl.text = winningLottery.winningNumbers[2].rank2Number
        viewHolder.dRank3Lbl.text = winningLottery.winningNumbers[2].rank3Number
        viewHolder.stRank1Lbl.text = winningLottery.winningNumbers[3].rank1Number
        viewHolder.stRank2Lbl.text = winningLottery.winningNumbers[3].rank2Number
        viewHolder.stRank3Lbl.text = winningLottery.winningNumbers[3].rank3Number
    }

    override fun getItemCount() = dataSet.size

    fun updateAdapter(newList: MutableList<WinningLottery>) {
        dataSet.clear()
        dataSet.addAll(newList)
        notifyDataSetChanged()
    }
}